//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace Preflight.Core
{
    using System;
    using Model;

    internal class VerificationProxy : IVerification 
    {
        private readonly IVerification verification;

        public VerificationProxy(IVerification verification)
        {
            this.verification = verification;
        }

        public string Name
        {
            get
            {
                return this.TryGetProperty("Name", verificationItem => verificationItem.Name, this.Verification.GetType().Name);
            }
        }

        public string Category
        {
            get
            {
                return this.TryGetProperty("Category", verificationItem => verificationItem.Category, "Unknown");
            }
        }

        public IVerification Verification
        {
            get { return this.verification; }
        }

        public VerificationResult Run()
        {
            try
            {
                var result = this.Verification.Run();

                if (result == null)
                {
                    Log.LogError("Verification {0} has returned a null result. This is not allowed thus this verification is marked as failed.", this.verification.Name);
                    var verificationResult = new VerificationResult(this);
                    verificationResult.AddMessage(MessageType.Error, Messages.VerificationHasReturnedNullResult);
                    return verificationResult;
                }

                return result;
            }
            catch (Exception exception)
            {
                Log.LogError(
                    "Run Verification has thrown an exception on verification: {0}. This is not recommended. Run method should catch exceptions and report failure details. Exception: {1}",
                    this.Verification.GetType(),
                    exception);

                var result = new VerificationResult(this);
                result.AddMessage(MessageType.Error, Messages.UnhandledVerificationException);
                result.AddException(exception);

                return result;
            }
        }

        private string TryGetProperty(string propertyName, Func<IVerification, string> action, string defaultValue)
        {
            try
            {
                var value = action(this.Verification);
                if (string.IsNullOrWhiteSpace(value))
                {
                    Log.LogError("You have a verification which has a null or empty {0} value", propertyName);
                    return defaultValue;
                }

                return value;
            }
            catch (Exception exception)
            {
                Log.LogError(
                    "Error occured while trying to access property: {0} on verification: {1}. Exception: {2}",
                    propertyName,
                    this.Verification.GetType(),
                    exception);

                return defaultValue;
            }
        }
    }
}
